package org.example.service.impl;


import org.example.mapper.ProductMapper;
import org.example.mapper.ProductTagMapper;
import org.example.pojo.Product;
import org.example.pojo.ProductTag;
import org.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    private final ProductMapper productMapper;
    private final ProductTagMapper productTagMapper;

    @Autowired
    public ProductServiceImpl(ProductMapper productMapper, ProductTagMapper productTagMapper) {
        this.productMapper = productMapper;
        this.productTagMapper = productTagMapper;
    }

    // 查询所有商品
    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    // 根据id查询商品
    @Override
    public Product findById(Long id) {
        return productMapper.findById(id);
    }

    // 删除商品
    @Override
    public void deleteById(Long id) {
        productMapper.deleteById(id);
    }

    // 根据商品id查询商品标签
    @Override
    public List<ProductTag> findTagsByProductId(Long id) {
        return productTagMapper.findByProductId(id);
    }

    // 添加商品标签
    @Override
    public ProductTag addTag(ProductTag tag) {
        productTagMapper.insert(tag);
        return tag;
    }
}
